(ns ura.app
  (:require
   [reagent.core :as r]
   [reagent.dom :as rd]
   [stylefy.core :as stylefy]
   [stylefy.reagent :as stylefy-reagent]
   [reitit.frontend :as rf]
   [reitit.frontend.easy :as rfe]
   [reitit.coercion.spec :as rss]
   [reitit.frontend.controllers :as rfc]
   
   [devtools.core :as devtools]

   [ura.common :as uc :refer [log spy]]
   
   [taoensso.telemere :as t]

   [ura.Org]
   [ura.User]
   [ura.Role]
   [ura.Auth]
   [ura.Log]
   [ura.Header]
   [ura.MenuSide]
   [ura.Footer]
   [ura.test]
   [ura.login]
   [ura.style :refer [attr]]
   ))

(defonce match (r/atom nil))

;; 404 页面组件
(defn not-found-page []
  [:div "404 - 未找到页面"])

(def current-page (r/atom :user))

(defn reset-home []
  (reset! current-page :user)
  )
;; 主页面组件
(defn main-page [subpage]
  (fn [subpage]
      (log "get sub" subpage @match)
      [:div (attr :display=grid :height=100vh
              [:grid-template-columns "230px auto 230px"]
              [:grid-template-rows    "56px  auto 22px"]
              [:grid-template-areas "'hd hd hd'
                                     'sb bd bd'
                                     'sb ft ft'"])
       ;; header
       [:div (attr :grid-area=hd
                   :color=white
                   :bgc=#04458A
                   [:box-shadow "0px 4px 4px rgba(0,0,0, 0.3)"])
        [ura.Header/header reset-home]
        ]

       ;; side bar
       [:div (attr :grid-row=sb :bgc=#04458A [:box-shadow "3px 0px 3px rgba(0,0,0, 0.2)"])
        [ura.MenuSide/side-menu @match]
        ]

       ;; body part
       [:div (attr :grid-area=bd :overflow=auto :mx=20 :my=10)
        (let [subp (:path @match)]
          (case subp
            "/user" [ura.User/genPage]
            "/org"  [ura.Org/genPage]
            "/role" [ura.Role/genPage]
            "/auth" [ura.Auth/genPage]
            "/log"  [ura.Log/genPage]
             [ura.User/genPage]
            )
          )
        ]

       ;; foot
       [:div (attr :grid-area=ft :bgc=#bdf [:box-shadow "3px 0px 3px rgba(0,0,0, 0.2)"])
        [ura.Footer/footer]
        ]
       ]
      ))

;; 路由配置
(def routes
  ["/"
   [""      {:name :main-page :view main-page}]
   ["login" {:name :login-page :view ura.login/loginPage}]
   ["404"   {:name :not-found-page :view not-found-page}]
   
   ["user"  {:name :user :view main-page}]
   ["org"   {:name :org  :view main-page}]
   ["role"  {:name :role :view main-page}]
   ["auth"  {:name :auth :view main-page}]
   ["log"   {:name :log  :view main-page}]
   ])

;; 初始化路由
(defonce history (r/atom nil))

(defn start-router! []
  (rfe/start!
   (rf/router routes)
    (fn [new-match]
      (reset! match new-match))
    {:use-fragment true}))


(defn app []
  (let [current-view (get-in @match [:data :view])]
    [:div ;; 顶层页面根
     (when current-view [current-view @match])
     ]
    ))


(defn initApp []
  (devtools/set-pref!
   :max-print-level 1 ;; 设定console里的log的对象展开层数
   )
  (devtools/install! [:formatters :hints])
  (stylefy/init {:dom (stylefy-reagent/init)})
  (t/add-handler! :default1 t/handler:console-raw)
  )

(defn ^:export run []
  (initApp)
  (start-router!)
  (rd/render [app] (js/document.getElementById "root")))
    
